package cn.iocoder.yudao.module.picturebook.controller.admin.book;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.book.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.book.BookDO;
import cn.iocoder.yudao.module.picturebook.service.book.BookService;

@Tag(name = "绘本系统 - 书籍信息")
@RestController
@RequestMapping("/picturebook/book")
@Validated
@Slf4j
public class BookController {

    @Resource
    private BookService bookService;

    @PostMapping("/create")
    @Operation(summary = "创建书籍信息")
    @PreAuthorize("@ss.hasPermission('picturebook:book:create')")
    public CommonResult<Long> createBook(@Valid @RequestBody BookSaveReqVO createReqVO) {
        return success(bookService.createBook(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新书籍信息")
    @PreAuthorize("@ss.hasPermission('picturebook:book:update')")
    public CommonResult<Boolean> updateBook(@Valid @RequestBody BookSaveReqVO updateReqVO) {
        bookService.updateBook(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除书籍信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:book:delete')")
    public CommonResult<Boolean> deleteBook(@RequestParam("id") Long id) {
        bookService.deleteBook(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除书籍信息")
    @PreAuthorize("@ss.hasPermission('picturebook:book:delete')")
    public CommonResult<Boolean> deleteBookList(@RequestParam("ids") List<Long> ids) {
        bookService.deleteBookListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得书籍信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:book:query')")
    public CommonResult<BookRespVO> getBook(@RequestParam("id") Long id) {
        BookDO book = bookService.getBook(id);
        return success(BeanUtils.toBean(book, BookRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得书籍信息分页")
    @PreAuthorize("@ss.hasPermission('picturebook:book:query')")
    public CommonResult<PageResult<BookRespVO>> getBookPage(@Valid BookPageReqVO pageReqVO) {
        PageResult<BookDO> pageResult = bookService.getBookPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BookRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出书籍信息 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:book:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBookExcel(@Valid BookPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookDO> list = bookService.getBookPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "书籍信息.xls", "数据", BookRespVO.class,
                BeanUtils.toBean(list, BookRespVO.class));
    }

    //根据leveL获取书籍信息
    @GetMapping("/get-by-level")
    @Operation(summary = "根据level获取书籍信息")
    public CommonResult<List<BookRespVO>> getBookByLevel(@RequestParam("level") Integer level) {
        // 参数校验
        if (level == null) {
            return CommonResult.error(400, "level参数不能为空");
        }

        try {
            List<BookDO> bookList = bookService.getBookByLevel(level);
            // 空值安全处理
            if (bookList == null) {
                bookList = new ArrayList<>();
            }
            return success(BeanUtils.toBean(bookList, BookRespVO.class));
        } catch (Exception e) {
            // 异常处理
            log.error("根据level获取书籍信息失败，level: {}", level, e);
            return CommonResult.error(500, "服务器内部错误");
        }
    }


    //创建绘本
    @PostMapping("/createPictureBook")
    @Operation(summary = "创建绘本")
    public CommonResult<Long> createPictureBook(@Valid @RequestBody Book createReqVO) {
        return success(bookService.createPictureBook(createReqVO));
    }
}